﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QualityRpt.Dao;
using System.Data;
using QualityRpt.Utils;

namespace QualityRpt.Service
{
    public class RPTResultService:BaseService
    {
        private static IRPTResultDao _impDao = null;
        static RPTResultService()
        {
            _impDao = CreateDaoInstance("IRPTResultDao") as IRPTResultDao;
        }
        public static DataTable GetTempSchema()
        {
            return _impDao.GetTempSchema();
        }
        public static DataTable GetTempMetaSchema()
        {
            return _impDao.GetTempMetaSchema();
        }
        public static DataTable GetTempMetaList(string where, string sort)
        {
            return _impDao.GetTempMetaList(where, sort);
        }
        public static DataRow SaveOBUTemplate(DataRow row,DataTable metaTable)
        {
            row["DEVICE_ID"] = DeviceService.OBU_DEV;
            DataRow rtRow = _impDao.SaveTemplate(row);
            _impDao.SaveTempMeta(Convert.ToInt32(rtRow["ID"]), metaTable);
            return rtRow;
        }
        public static DataRow SaveAIRTemplate(DataRow row,DataTable metaTable)
        {
            row["DEVICE_ID"] = DeviceService.AIRWIRE_DEV;
            DataRow rtRow =  _impDao.SaveTemplate(row);
            _impDao.SaveTempMeta(Convert.ToInt32(rtRow["ID"]), metaTable);
            return rtRow;
        }
        public static DataRow SaveCARDTemplate(DataRow row,DataTable metaTable)
        {
            row["DEVICE_ID"] = DeviceService.ICARD_DEV;
            DataRow rtRow = _impDao.SaveTemplate(row);
            _impDao.SaveTempMeta(Convert.ToInt32(rtRow["ID"]), metaTable);
            return rtRow;
        }
        public static void UpdateTemplate(DataRow row,DataTable metaTable)
        {
            _impDao.UpdateTemplate(row);
            _impDao.UpdateTempMeta(Convert.ToInt32(row["ID"]), metaTable);
        }
        public static void DelTemplate(int id)
        {
            _impDao.DelTemplate(id);
        }
        public static DataTable GetTempPageList(PageUtils pu, string where, string sort)
        {
            return _impDao.GetTempPageList(pu, where, sort);
        }
        public static DataTable GetSchema()
        {
            return _impDao.GetList("ID=0");
        }
        public static DataTable Save(int job_id,DataTable result_table)
        {
            _impDao.DeleteByJobID(job_id);
            return _impDao.Save(job_id, result_table);
        }
        public static void DeleteByJobID(int job_id)
        {
            _impDao.DeleteByJobID(job_id);
        }
        public static DataTable GetByJob(int job_id)
        {
            return _impDao.GetByJobID(job_id);
        }
        /// <summary>
        /// 将结果格式化为比较
        /// </summary>
        /// <param name="temp_id"></param>
        /// <param name="rTable"></param>
        /// <returns></returns>
        public static DataTable FormatResult(int temp_id, DataTable rTable)
        {
            for (int i = rTable.Columns.Count - 1; i >= 0; i--)
            {
                bool hasValue = false;
                foreach (DataRow r in rTable.Rows)
                {
                    if (!r.IsNull(rTable.Columns[i]))
                    {
                        hasValue = true;
                        break;
                    }
                }
                if (!hasValue)
                {
                    rTable.Columns.RemoveAt(i);
                }
            }
            if(rTable.Columns.Contains("ID"))
                rTable.Columns.Remove("ID");
            if (rTable.Columns.Contains("JOB_ID"))
                rTable.Columns.Remove("JOB_ID");
            rTable.AcceptChanges();
            return rTable;
        }
        public static DataTable GetPageList(PageUtils pu, string where, string sort)
        {
            return _impDao.GetPageList(pu, where, sort);
        }
        public static DataRow GetTemplate(int id)
        {
            return _impDao.GetTemplate(id);
        }
        public static DataTable GetTempMetaList(int id)
        {
            return _impDao.GetTempMetaList(id);
        }
    }
}
